﻿using RiskMan.Config;
using RiskMan.Providers.Channels;
using WpfApplication1.Providers;
using XlDde;

namespace RiskMan.Providers
{
    public class DdeProvider
    {
        private XlDdeServer _server;
        public bool IsConnected = false;

        public void Connect()
        {
            _server = new XlDdeServer(cfg.DdeServer);
            
            _server.AddChannel(cfg.DdeChannelCurrentList, new DDEChannelCurrentList( ) );
            _server.AddChannel(cfg.DdeChannelDealsList, new DDEChannelDealList());
            _server.AddChannel(cfg.DdeChannelLimiteToEmitent, new DDEChannelTableLimiteByEmitent());
            _server.AddChannel(cfg.DdeChannelPositionToClientAccount, new DDEChannelPositionToClientAccount());
            _server.AddChannel(cfg.DdeChannelStopOrders, new DDEChannelStopOrderList() );
            _server.AddChannel(cfg.DdeChannelOrders, new DDEChannelOrderList());
            _server.AddChannel(cfg.DdeChannelLimiteByAccount, new DDEChannelLimiteByAccount());
            _server.AddChannel(cfg.DdeChannelportfel, new DDEChannelPortfel());
            _server.AddChannel(cfg.DdeLimeiteByCash, new DDEChannelMoneySredstva());

            _server.Register();
            IsConnected = true;
        }

        public void Disconnect()
        {
            if (_server != null)
            {
                _server.Unregister();
                _server.Disconnect();
                _server.Dispose();
                _server = null;
                IsConnected = false;
            }
        }
    }
}
